
Partial Class Admin_AssessmentActivities
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ClearStatus()
        SetupJavaScript()
        If Not Page.IsPostBack Then
            Me.BindGrid()
        End If

    End Sub

    Private Sub SetupJavaScript()
        EntityController.JavaScriptAddChildDiv(Me.PageDiv.ClientID, 100)
        EntityController.JavaScriptSetFixedHeight(30)
    End Sub

    Protected Sub BindGrid()

        Me.AssessmentActivityGridView.DataSource = Cdm.RIDataCore.Assessments.AssessmentActivityDisplay.ListBySort("Description")
        Me.AssessmentActivityGridView.DataBind()

    End Sub

    Protected Sub AssessmentActivityGridView_RowCancelingEdit(ByVal sender As Object, _
                                                              ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) _
                                                              Handles AssessmentActivityGridView.RowCancelingEdit

        ' Exit edit mode.
        Me.AssessmentActivityGridView.EditIndex = -1
        SetStatus("Edit Cancelled.")
        Me.BindGrid()

    End Sub

    Protected Sub AssessmentActivityGridView_RowCommand(ByVal sender As Object, _
                                                        ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
                                                        Handles AssessmentActivityGridView.RowCommand

        ' Handle delete button click.
        If e.CommandName = "DeleteActivity" Then
            Cdm.RIDataCore.Assessments.AssessmentActivity.Retrieve(e.CommandArgument).Remove()
            SetStatus("Activity Deleted.")
            Me.BindGrid()
        End If

    End Sub

    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub
    Private Sub ClearStatus()
        SetStatus("")
    End Sub

    Protected Sub AssessmentActivityGridView_RowDataBound(ByVal sender As Object, _
                                                          ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
                                                          Handles AssessmentActivityGridView.RowDataBound

        ' Get a reference to the activity name field that is in edit mode and set focus to it.
        Dim ActivityNameTextBox As TextBox = CType(e.Row.FindControl("ActivityNameTextBox"), TextBox)
        If Not ActivityNameTextBox Is Nothing Then
            SetFocus(ActivityNameTextBox.ClientID)
        End If

        If e.Row.RowType = DataControlRowType.DataRow Then
            ' Determine whether the delete button should be enabled or not.
            CType(e.Row.Cells(3).Controls(1), LinkButton).Enabled = (Not Cdm.RIDataCore.Assessments.AssessmentActivityValue.AssessmentActivityValueExists(Me.AssessmentActivityGridView.DataKeys(e.Row.RowIndex).Values(0)))
        End If

    End Sub

    Protected Sub AssessmentActivityGridView_RowEditing(ByVal sender As Object, _
                                                        ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) _
                                                        Handles AssessmentActivityGridView.RowEditing

        ' Put the gridview in edit mode.
        Me.AssessmentActivityGridView.EditIndex = e.NewEditIndex
        Me.BindGrid()

        ' Populate the assessment template drop down list.
        Dim ddl As DropDownList = CType(Me.AssessmentActivityGridView.Rows(e.NewEditIndex).Cells(1).Controls(1), DropDownList)
        ddl.DataSource = Cdm.RIDataCore.Assessments.AssessmentTemplate.ListAll
        ddl.DataTextField = "ControlName"
        ddl.DataValueField = "AssessmentTemplateId"
        ddl.DataBind()
        ddl.SelectedValue = Me.AssessmentActivityGridView.DataKeys(e.NewEditIndex).Values(1)
        ddl.Attributes.Add("onchange", "javascript:")

    End Sub

    Protected Sub AssessmentActivityGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles AssessmentActivityGridView.RowUpdated
        SetStatus("Activity Updated.")
    End Sub

    Protected Sub AssessmentActivityGridView_RowUpdating(ByVal sender As Object, _
                                                         ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
                                                         Handles AssessmentActivityGridView.RowUpdating

        ' Save new values.
        Dim ddl As DropDownList = CType(Me.AssessmentActivityGridView.Rows(e.RowIndex).Cells(1).Controls(1), DropDownList)
        Dim assessmentActivity As Cdm.RIDataCore.Assessments.AssessmentActivity
        assessmentActivity = Cdm.RIDataCore.Assessments.AssessmentActivity.Retrieve(Me.AssessmentActivityGridView.DataKeys(e.RowIndex).Values(0))
        assessmentActivity.Description = CType(Me.AssessmentActivityGridView.Rows(e.RowIndex).Cells(0).Controls(1), TextBox).Text
        assessmentActivity.AssessmentTemplateId = ddl.SelectedValue
        assessmentActivity.LastModified = UserAuthorization.CurrentUser.LogonId
        assessmentActivity.Persist()

        ' Exit edit mode.
        Me.AssessmentActivityGridView.EditIndex = -1
        Me.BindGrid()

    End Sub

    Protected Sub AddNewButton_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles AddNewButton.Click

        Dim imageName As String

        ' Load drop down lists and switch to add new view.
        With Me.NewAssessmentCategoryDropDownList
            .DataSource = Cdm.RIDataCore.Assessments.AssessmentCategory.ListAll
            .DataValueField = "AssessmentCategoryId"
            .DataTextField = "Description"
            .DataBind()
        End With

        With Me.NewTemplateDropDownList
            .DataSource = Cdm.RIDataCore.Assessments.AssessmentTemplate.ListAll
            .DataValueField = "AssessmentTemplateId"
            .DataTextField = "ControlName"
            .DataBind()
            .Attributes.Add("onchange", "javascript: ToggleTemplateImage('" & Me.NewTemplateDropDownList.ClientID & "', '" & Me.TemplateImage.ClientID & "'); return false;")
        End With

        imageName = CType(Me.NewTemplateDropDownList.SelectedItem.ToString.Split("."), Array)(0)
        Me.TemplateImage.ImageUrl = "~/images/" & imageName & ".gif"

        Me.ActivitiesPanel.Visible = False
        Me.TemplateKeyPanel.Visible = False
        Me.AddNewPanel.Visible = True
        Me.AddNewButton.Visible = False

    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles SaveButton.Click

        ' Start a transaction to ensure all pieces get created.
        Dim tran As New Gentle.Framework.Transaction

        Try

            ' Create a new activity.
            Dim activity As New Cdm.RIDataCore.Assessments.AssessmentActivity(Me.NewAssessmentCategoryDropDownList.SelectedValue, _
                                                                              Me.NewTemplateDropDownList.SelectedValue, _
                                                                              Me.NewActivityNameTextBox.Text, _
                                                                              UserAuthorization.CurrentUser.LogonId)
            activity.Persist(tran)

            ' Create a new activity cost.
            Dim activityCost As New Cdm.RIDataCore.Assessments.AssessmentActivityTime(activity.AssessmentActivityId, _
                                                                                      IIf(IsNumeric(Me.CostTextBox.Text), Me.CostTextBox.Text, -1), _
                                                                                      IIf(IsNumeric(Me.FunctionalCostTextBox.Text), Me.FunctionalCostTextBox.Text, -1), _
                                                                                      EffectiveDateCalendar.SelectedDate, _
                                                                                      Nothing, _
                                                                                      UserAuthorization.CurrentUser.LogonId)
            activityCost.Persist(tran)

            ' Save all.
            tran.Commit()

            ' Exit add new view.
            Me.NewActivityNameTextBox.Text = ""
            Me.ActivitiesPanel.Visible = True
            Me.TemplateKeyPanel.Visible = False
            Me.AddNewPanel.Visible = False
            Me.AddNewButton.Visible = True
            SetStatus("Activity Added.")
            Me.BindGrid()

        Catch ex As Gentle.Common.GentleException
            SetStatus("error Adding:" + ex.Message)
            tran.Rollback()

        End Try

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles CancelButton.Click

        ' Exit add new view.
        Me.NewActivityNameTextBox.Text = ""
        Me.ActivitiesPanel.Visible = True
        Me.TemplateKeyPanel.Visible = False
        Me.AddNewPanel.Visible = False
        Me.AddNewButton.Visible = True
        SetStatus("Action Cancelled.")
    End Sub

    Protected Sub ViewTemplatesButton_Click(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles ViewTemplatesButton.Click

        ' Show a view that represents what each template looks like.
        Me.ActivitiesPanel.Visible = False
        Me.TemplateKeyPanel.Visible = True
        Me.AddNewPanel.Visible = False
        Me.AddNewButton.Visible = True

    End Sub

    Protected Sub CloseTemplateKeyButton_Click(ByVal sender As Object, _
                                               ByVal e As System.EventArgs) _
                                               Handles CloseTemplateKeyButton.Click

        Me.AddNewButton.Visible = True
        Me.ActivitiesPanel.Visible = True
        Me.TemplateKeyPanel.Visible = False
        Me.AddNewPanel.Visible = False
        Me.AddNewButton.Visible = True

    End Sub

End Class
